﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileFilter
{
    public class Filter : IFilter
    {
        private const int    FILTER_ID = 1000;
        private const string FILTER_VER = "1.0.0";
        private const string FILTER_DESC = 
            "This is a exemple of a filter.\n" +
            "This filter is used to transfer files.";

        public int GetID()
        {
            return FILTER_ID;
        }

        public string GetVersion()
        {
            return FILTER_VER;
        }

        public string GetDescription()
        {
            return FILTER_DESC;
        }

        public void Parse(int length, int id, byte[] data)
        {
            string str = "";

            for (int i = 0; i < length; i++)
            {
                str = str + (char)data[i];
            }
            Console.WriteLine("Data from client: " + str);
            //// data: { name_len }{ name }{ file }
            //int          fileNameLen;       // the lenght of the file name
            //string       fileName;          // the name of the file
            //BinaryWriter bFile;             // binary writer for the file

            //fileNameLen = BitConverter.ToInt32(data, 0);
            //fileName = Encoding.ASCII.GetString(data, 4, fileNameLen);

            //bFile = new BinaryWriter(
            //    File.Open("C:\\" + fileName, FileMode.Append) );

            //bFile.Write(data,
            //             4 + fileNameLen,
            //             data.Length - 4 - fileNameLen);
        }

        public bool Validate()
        {
            // nothing to validate ...
            return true;
        }

        public void SetServerFunctions(object sf)
        {
            // not yet implemented
            throw new NotImplementedException();
        }

        public bool CanProcess(int id)
        {
            switch (id)
            {
                case 101:
                case 102:
                    return true;             
                default:
                    return false;
            }
        }
    }
}
